#include "register.h"
#include "ui_register.h"
#include<QMessageBox>
Register::Register(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Register)
{
    ui->setupUi(this);

    //设置用户名
    ui->lineEdit->setPlaceholderText("请输入用户名");
    //设置密码
    ui->lineEdit_2->setPlaceholderText("请输入密码");
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    //设置姓名
    ui->lineEdit_3->setPlaceholderText("请输入姓名");

    //设置年龄
    ui->spinBox->setRange(1,100);
    ui->spinBox->setSingleStep(1);
    //设置性别
    ui->comboBox->addItem("男");
    ui->comboBox->addItem("女");
}

Register::~Register()
{
    delete ui;
}


void Register::on_pushButton_2_clicked()
{
    this->close();
}

void Register::on_pushButton_clicked()
{
    if(ui->lineEdit->text()==NULL||ui->lineEdit_2->text()==NULL||ui->lineEdit_3->text()==NULL){
        QMessageBox::information(this,"提示信息","请填写完整信息");
    }else{
        // 获取用户输入的数据
        QString username = ui->lineEdit->text();
        QString password = ui->lineEdit_2->text();
        QString name = ui->lineEdit_3->text();
        QString role = QString::number(DOCTOR);
        int age = ui->spinBox->text().toInt();
        QString gender = ui->comboBox->currentText();
        QString contactNumber = ui->lineEdit_4->text();
        QString email = ui->lineEdit_5->text();
        // 构建要发送的数据
        QString data = username + "," + password + ","  + name + "," + role + "," + QString::number(age) + ","+ gender + "," + contactNumber + "," + email;
        QString sql = QString("INSERT INTO Users (Username, Password, Role, Name, Gender, Age, ContactNumber, Email) "
                              "VALUES ('%1', '%2', '%3', '%4', '%5', '%6', %7, '%8')")
                .arg(username)
                .arg(password)
                .arg("Patient")
                .arg(name)
                .arg(gender)
                .arg(age)       // 假设 age 是整型
                .arg(contactNumber)
                .arg(email);

        tcp = new ClientTcp(INSERT_DATA);

        tcp -> sendMsg(sql);

        connect(tcp->socket,&QTcpSocket::readyRead,[=]() {
            // 等待服务器响应Insert successful
            if (tcp->getFlag()) {
                QMessageBox::information(this, "注册", "Registration successful!");
            } else {
                QMessageBox::warning(this, "注册", "Registration failed!" + tcp->getRec());
            }

        });

    }
}
